local CombineController = {
    -- 初始化合成预览预设
    previewUnits = {},
    combineData = require("Data.CombineData") -- 合成配置数据
}

function CombineController:init()
    -- 注册自定义事件
    LuaAPI.global_register_custom_event("点击合成", function(_, _, data)
        local role = data.role
        local combineInfo = self.combineInfos[role.get_roleid()]
        if self:checkCanCombine(role, combineInfo) then
            self:tryCombineItems(role, combineInfo)
        end
    end)

    -- 为每个玩家创建合成预览
    for _, role in ipairs(GameAPI.get_all_valid_roles()) do
        local roleId = role.get_roleid()
        self.previewUnits[roleId] = GameAPI.create_obstacle(
            UnitPrefab["合成预览"],
            math.Vector3(0, 0, 0),
            math.Quaternion(0, 0, 0),
            math.Vector3(1, 1, 1),
            role
        )
    end
end

function CombineController:tryCombineItems(role, combineInfo)
    -- 检查合成条件
    if combineInfo.target and combineInfo.items then
        -- 执行合成逻辑
        role.get_ctrl_unit().drop_items(combineInfo.items)
        
        -- 设置合成物品位置
        combineInfo.target.set_position(combineInfo.combinePos)
        combineInfo.target.set_orientation(combineInfo.combineRot or role.get_ctrl_unit().get_orientation())
        
        -- 添加合成标记
        combineInfo.target.set_kv_by_type(Enums.ValueType.Bool, "isCombined", true)
    end
end

return CombineController